<?php
namespace Home\Controller;
header('content-type:text/html;charset=utf-8');
use Think\Controller;
use Org\Type\CatTree;
class GoodsdetailController extends Controller {




    public function index(){

        //实例化商品分类
        $cats=M('Cats');
        //查询所有类别
        $allcats=$cats->field('catsid,catsname,pid,path')->select();
        //调用分类函数
        $type = CatTree::getlist($allcats);
        //为模板变量赋值
        $this->assign('type',$type);

        //实例化商品信息
        $good=M('Goods');


        //接受商品id
        $goodsid=I('get.goodsid');
        $goodsdetail=$good->where('goodsid='.$goodsid)->join('yg_cats ON yg_goods.pcats=yg_cats.catsid')->find();
        $goodsdetail['description'] = html_entity_decode($goodsdetail['description']);
        $pid=$goodsdetail['pcats'];
        //猜你喜欢 遍历相同的父id商品
        $go=$good->where('pcats='.$pid)
                    ->limit('4')
                    ->select();

        $this->assign('go',$go);
        //新品专区 条件state2=1
        $new=$good->where('state2=1')
                    ->limit('4')
                    ->select();

        $this->assign('new',$new);
        $here = $this->now_here($goodsdetail['catsid']);
        $here .= "> <a href='#'>$goodsdetail[goodsname]</a>";
        $this->assign('here',$here);


        $gpath=$cats->field('catsname')->where('catsid='.$goodsdetail['pid'])->find();
        //商品顶级分类
        $goodsdetail['gpath']=$gpath['catsname'];
        //商品编号
        $goodsdetail['goodsNo']=($goodsdetail['goodsid'])+10000;
        //商品市场价
        $goodsdetail['oldprice']=ceil(($goodsdetail['price'] * 10 / 8));
        //商品类型
        $goodsdetail['goodsNum']=I('get.Num');


        //分配到模板
        $this->assign('goodsdetail',$goodsdetail);


        //实例化评价表
        $comment=M('comment');

        //链接回复表
        $comments=$comment->field('a.*,b.id as rid,b.commit_id as comid,b.replay_content as rcon,b.addtime as rtime,b.user_id as ruser')
                          ->alias('a')
                          ->join('LEFT JOIN yg_commentreplay as b on a.commit_id=b.commit_id')
                          ->where(array('goods_id'=>$goodsid))
                          ->select();

        $this->assign('comment',$comments);
        $this->display();

    }
}